#include "sc_car_def_manager.h"
#include <cassert>

ScWheelDef* ScCarDefManager::createWheel( const std::string& id, const float radius, const float width, const float friction, const std::string& objPath)
{
	const WheelMap::iterator iterator=m_wheelMap.find(id);
	const bool condition=iterator==m_wheelMap.end();
	assert(condition);
	if(condition)
	{
		m_wheelMap[id] = boost::shared_ptr<ScWheelDef>( new ScWheelDef(id, radius, width, friction, objPath) );
		return m_wheelMap[id].get();
	}
	return 0;
}

const ScWheelDef* ScCarDefManager::getWheel( const std::string& id ) const
{
	const WheelMap::const_iterator iterator=m_wheelMap.find(id);
	return iterator!=m_wheelMap.end() ? (*iterator).second.get() : 0;
}

ScSuspensionDef* ScCarDefManager::createSuspension( const std::string& id, const float stiffness, const float damping, const float compression, const float length, const float rollInfluence, const float maximumForce )
{
	const SuspensionMap::iterator iterator=m_suspensionMap.find(id);
	const bool condition=iterator==m_suspensionMap.end();
	assert(condition);
	if(condition)
	{
		m_suspensionMap[id] = boost::shared_ptr<ScSuspensionDef>( new ScSuspensionDef(id, stiffness, damping, compression, length, rollInfluence, maximumForce) );
		return m_suspensionMap[id].get();
	}
	return 0;
}

const ScSuspensionDef* ScCarDefManager::getSuspension( const std::string& id ) const
{
	const SuspensionMap::const_iterator iterator=m_suspensionMap.find(id);
	return iterator!=m_suspensionMap.end() ? (*iterator).second.get() : 0;
}

ScEngineDef* ScCarDefManager::createEngine( const std::string& id, const float force )
{
	const EngineMap::iterator iterator=m_engineMap.find(id);
	const bool condition=iterator==m_engineMap.end();
	assert(condition);
	if(condition)
	{
		m_engineMap[id] = boost::shared_ptr<ScEngineDef>( new ScEngineDef(id, force) );
		return m_engineMap[id].get();
	}
	return 0;
}

const ScEngineDef* ScCarDefManager::getEngine( const std::string& id ) const
{
	const EngineMap::const_iterator iterator=m_engineMap.find(id);
	return iterator!=m_engineMap.end() ? (*iterator).second.get() : 0;
}

ScBrakesDef* ScCarDefManager::createBrakes( const std::string& id, const float force )
{
	const BrakesMap::iterator iterator=m_brakesMap.find(id);
	const bool condition=iterator==m_brakesMap.end();
	assert(condition);
	if(condition)
	{
		m_brakesMap[id] = boost::shared_ptr<ScBrakesDef>( new ScBrakesDef(id, force) );
		return m_brakesMap[id].get();
	}
	return 0;
}

const ScBrakesDef* ScCarDefManager::getBrakes( const std::string& id ) const
{
	const BrakesMap::const_iterator iterator=m_brakesMap.find(id);
	return iterator!=m_brakesMap.end() ? (*iterator).second.get() : 0;
}

ScChassisDef* ScCarDefManager::createChassis(const std::string& id, const Vector3f& halfDimensions, const float mass, const ScChassisDef::WheelPairAttachments wheelPairAttachments, const std::string& objPath)
{
	const ChassisMap::iterator iterator=m_chassisMap.find(id);
	const bool condition=iterator==m_chassisMap.end();
	assert(condition);
	if(condition)
	{
		m_chassisMap[id] = boost::shared_ptr<ScChassisDef>( new ScChassisDef(id, halfDimensions, mass, wheelPairAttachments, objPath) );
		return m_chassisMap[id].get();
	}
	return 0;
}

const ScChassisDef* ScCarDefManager::getChassis( const std::string& id ) const
{
	const ChassisMap::const_iterator iterator=m_chassisMap.find(id);
	return iterator!=m_chassisMap.end() ? (*iterator).second.get() : 0;
}

ScCarDef* ScCarDefManager::createCar( const std::string& id, const ScChassisDef& chassisDef, const ScEngineDef& engineDef, const ScBrakesDef& brakesDef )
{
	const CarMap::iterator iterator=m_carMap.find(id);
	const bool condition=iterator==m_carMap.end();
	assert(condition);
	if(condition)
	{
		m_carMap[id] = boost::shared_ptr<ScCarDef>( new ScCarDef(id, engineDef, brakesDef, chassisDef) );
		return m_carMap[id].get();
	}
	return 0;
}

const ScCarDef* ScCarDefManager::getCar( const std::string& id ) const
{
	const CarMap::const_iterator iterator=m_carMap.find(id);
	return iterator!=m_carMap.end() ? (*iterator).second.get() : 0;
}

ScCarDefManager& ScCarDefManager::instance()
{
	static ScCarDefManager instance;
	return instance;
}
